package com.kxq.zhuoyue.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.pageUtilEntity.Page;
import com.kxq.zhuoyue.po.Program;
import com.kxq.zhuoyue.service.ProgramService;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-01
 */
@RestController
@RequestMapping("/program")
public class ProgramController {

    @Autowired
    private ProgramService programService;

    /**
     * 查询
     * @param page
     * @return
     */
    @PostMapping("/getByTitle")
    public AjaxResult getByTitle(@RequestBody Page page){
        Map map = new HashMap<>();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        LambdaQueryWrapper<Program> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Program::getProgramTitle,page.getProgramTitle());
        List<Program> list = programService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo<>(list);
        map.put("data",list);
        map.put("pageSize",pageInfo.getPageSize());
        map.put("total",pageInfo.getTotal());
        return AjaxResult.success("查询成功 !",map);
    }

    /**
     * 添加
     * @param program
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Program program){
        program.setProgramStatus("未审核 ！");
        boolean save = programService.save(program);
        if (save){
            return AjaxResult.success("添加成功 ！");
        }
        else {
            return AjaxResult.error("添加失败 ！");
        }
    }

    /**
     * 更新
     * @param program
     * @return
     */
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Program program){
        boolean update = programService.updateById(program);
        if (update){
            return AjaxResult.success("修改成功 ！");
        }
        else {
            return AjaxResult.error("修改失败 ！");
        }
    }

    /**
     * 删除
     * @param programId
     * @return
     */
    @DeleteMapping("/delete/{programId}")
    public AjaxResult delete (@PathVariable String programId){
        boolean b = programService.removeById(programId);
        if (b){
            return AjaxResult.success("删除成功 ！");
        }
        else {
            return AjaxResult.error("删除失败 ！");
        }
    }

}

